<?php
/**
 * This class is the parent of the majority of the database classes. All child classes e.g Products
 * call this classes __constructor at their construction. This sets up the screen processing as 
 * found below. 
 * @author andy.jackson
 *
 */
	class Form extends MySqlResults
	{
		protected  $_boDebug;
		protected  $_nextScreen 	= null;
		protected  $_problemScreen	= null;
		protected  $_redirectScreen = null;
		protected  $_boFailedValidation;
		protected  $_Message;
		
		/**
		 * Set-up which pages to display on particular events.
		 * @var $_REQUEST['SCREEN'] this is the next page to display on successful processing
		 * @var $_REQUEST['txtProblemScreen'] this is the page to display if any processing fails
		 * @var $_REQUEST['redirect'] page that gets redirected to after successful display of SCREEN
		 */
		function __construct()
		{
			/*  
			 * Need to have parent::__construct(); as top line of child __constructor for
			 * the following to be set-up correctly 
			 */
			if(isset($_REQUEST['SCREEN']))
			{
				$this->_nextScreen 		= $_REQUEST['SCREEN'];
				$this->_problemScreen   = $_REQUEST['SCREEN'];
			}
			
			if(isset($_REQUEST['txtProblemScreen']))
				$this->_problemScreen 	= $_REQUEST['txtProblemScreen'];
				
			if(isset($_REQUEST['redirect']))
				$this->_redirectScreen	= $_REQUEST['redirect'];
			
			$this->_boFailedValidation = false;
		}
		
		protected function _setNewValue(&$object, $strNewValue)
		{
			/* 
			 * $object passed in by reference (&$object) 
			 * alternatively $strProperty could be passed in
			 * and set using $this->{$strProperty}['value'] = ""
			 */
            $strNewValue = cleanFormInput($strNewValue);
			if($this->_boDebug)
			{
				echo "<br/>before update";
				print_array($object);
			}
			//Update values if 'new' value is different to current value
			if(!isset($object['value']))
				$object['value'] = "";
			if($object['value'] != $strNewValue)
			{
				$object['updated'] 	= true;
				$object['oldvalue'] = $object['value'];
				$object['value'] 	= $strNewValue;
			}
			if($this->_boDebug)
			{
				echo "<br/>after update";
				print_array($object);
			}
			return $object['value'];
		}
		
		function get_boFailedValidation(){
			return $this->_boFailedValidation;
		}
		
		
		/**
		 * function getDataObjectRow($strSQL, &$object)
		 * Return data row to the $object that was passed by reference 
		 * e.g
		 * $this passed in from Node class and $object->* is passed back into $this->*
		 * in Node class
		 * $object->email['value'] = "test@test.com"
		 * $object->email['updated'] = false
		 * $object->email['readonly'] = false
		 */
	    function getDataObjectRow($strSQL, &$object)
		{
			$this->MySqlResults($strSQL);
			if($this->numrows == 1)
			{
				for($iFieldCount = 0; $iFieldCount < $this->numfields ; $iFieldCount++)
				{					
					/* 
					 * fields that are created by query are prefixed with _ro_ to denote they
					 * won't be updateable via SQL
					 */
					$strProperty = "_".mysql_field_name($this->result, $iFieldCount);
					/* define array as otherwise get warning that $strProperty is_scalar */
				    $object->{$strProperty} = array();

					if(substr($strProperty,0,4) == "_ro_")
					{
						$strProperty = substr($strProperty, 4);
						$object->{$strProperty}['readonly'] = true;
					}else 
						$object->{$strProperty}['readonly'] = false;
					
					$object->{$strProperty}['value'] = $this->myrow[mysql_field_name($this->result, $iFieldCount)];
					$object->{$strProperty}['updated'] = false;
					$object->{$strProperty}['oldvalue'] = "";
				}		
	
				return true;
			}else 	
				return false;
		}
		
		function get_nextScreen(){
			return $this->_nextScreen;
		}
		
		function get_problemScreen(){
			return $this->_problemScreen;
		}
		
		function set_boDebug($boOn){
			return $this->_boDebug = $boOn;
		}
		
		function set_boFailedValidation($boFailedValidation){
			$this->_boFailedValidation = $boFailedValidation;
			return $this->_boFailedValidation;
		}
		
		/**
		 * Set next screen to problem screen (usually screen that executed called ACTION)
		 * and blank out screen redirection 
		 * @return bool
		 */
		function _setFailScreens($strFailMessage = "", $boFailValidation = false){
			$this->_nextScreen 	= $this->_problemScreen;
			$this->_redirectScreen 	= "";
			
			if($boFailValidation)
				$this->set_boFailedValidation(true);
				
			if($strFailMessage)
				$GLOBALS['arrActionResults'][] = $strFailMessage;
			$this->setTargetEnviroment();
			return false;
		}
		
		function set_nextScreen($strNextScreen){
			$this->_nextScreen = $strNextScreen;
			return $this->_nextScreen;
		}
		
		function set_problemScreen($strProblemScreen){
			$this->_problemScreen = $strProblemScreen;
			return $this->_problemScreen;
		}
		
		function setTargetScreen($strScreen, $strProblemScreen, $strRedirect){
			/* Manually set screens,  Normally set at construction */
			$this->_nextScreen 		= $strScreen;
			$this->_problemScreen 	= $strProblemScreen;
			$this->_redirectScreen	= $strRedirect;
		}
		
		function setTargetEnviroment(){
			$_REQUEST['SCREEN'] = $this->_nextScreen;
			$GLOBALS['SCREEN']	= $this->_nextScreen;
			$_REQUEST['redirect']	= $this->_redirectScreen;
		}
		
		function validateInput($strFunction, $strInput, $strFieldDescription)
		{
			if(!($strFunction(trim($strInput), $strFieldDescription)))
			{
				$this->set_boFailedValidation(true);
				return ($this->_setFailScreens($strFieldDescription));
			}else
			    return true;
		}
		
		function validateLength($iMax, $strInput, $strFieldDescription)
		{
			if(strlen($strInput > $iMax))
			{
				$GLOBALS['arrActionResults'][] = $strFieldDescription." needs to be less than ".($iMax + 1)." characters long";
				$this->set_boFailedValidation(true);
				return ($this->_setFailScreens());
			}else
			    return true;
		}
	}
?>